/**
 * Created with IntelliJ IDEA.
 * Description: 序列化二叉树
 * User: wangxin
 * Date: 2024-11-21
 * Time: 21:15
 */

import java.util.*;

 class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}

public class test1 {


        int INF = 0x3f3f3f3f;
        TreeNode emptyNode = new TreeNode(INF);
        String Serialize(TreeNode root) {
            if(root == null){
                return "";
            }
            Queue<TreeNode> q = new LinkedList<>();
            StringBuffer sb = new StringBuffer();
            q.offer(root);
            while(!q.isEmpty()){
                TreeNode cur = q.poll();
                sb.append(cur.val+",");
                if(!cur.equals(emptyNode)){
                    q.offer(cur.left != null?cur.left:emptyNode);
                    q.offer(cur.right != null?cur.right:emptyNode);
                }
            }
            return sb.toString();
        }
        TreeNode Deserialize(String str) {
            if("".equals(str)){
                return null;
            }
            String[] ss = str.split(",");
            int n = ss.length;
            TreeNode root = new TreeNode(Integer.parseInt(ss[0]));
            Queue<TreeNode> q = new LinkedList<>();
            q.offer(root);
            for(int i = 1;i<n-1;i+=2){
                TreeNode cur = q.poll();
                int a = Integer.parseInt(ss[i]),b = Integer.parseInt(ss[i+1]);
                if(a != INF){
                    cur.left = new TreeNode(a);
                    q.offer(cur.left);
                }
                if(b != INF){
                    cur.right = new TreeNode(b);
                    q.offer(cur.right);
                }
            }
            return root;
        }


}
